package com.leyou.item.service;

import com.leyou.common.exception.domain.ExceptionEnum;
import com.leyou.common.exception.domain.LyException;
import com.leyou.common.utils.BeanHelper;
import com.leyou.item.domian.SpecGroup;
import com.leyou.item.domian.SpecParam;
import com.leyou.item.dto.SpecGroupDTO;
import com.leyou.item.dto.SpecParamDTO;
import com.leyou.item.mapper.SpecGroupMapper;
import com.leyou.item.mapper.SpecParamMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service

public class SpecService {
    @Autowired
    private SpecGroupMapper specGroupMapper;
    @Autowired
    private SpecParamMapper specParamMapper;

    /**
     *根据类别查询规格组
     * @param id
     * @return
     */
    public List<SpecGroupDTO> findSpecGroupByCid(Long id) {
        SpecGroup specGroup=new SpecGroup();
        List<SpecGroup> specGroups = specGroupMapper.select(specGroup);
        if (CollectionUtils.isEmpty(specGroups)){
            throw new LyException(ExceptionEnum.SPEC_NOT_FOUND);
        }
        return BeanHelper.copyWithCollection(specGroups,SpecGroupDTO.class);


    }

    /**
     * 根据规格组查询规格参数
     * @param cid
     * @param gid
     * @param searching
     * @return
     */
    public List<SpecParamDTO> findSpecParam(Long cid, Long gid, Boolean searching) {
        //规格组和分类必须选一个
        if(gid==null &&cid==null){
            throw new LyException(ExceptionEnum.INVALID_PARAM_ERROR);
        }
        //设置传入的参数
        SpecParam specParam=new SpecParam();
        specParam.setCid(cid);
        specParam.setGroupId(gid);
        specParam.setSearching(searching);

        List<SpecParam> specParams = specParamMapper.select(specParam);
        if (CollectionUtils.isEmpty(specParams)){
            throw  new LyException(ExceptionEnum.INVALID_PARAM_ERROR);

        }
        return  BeanHelper.copyWithCollection(specParams,SpecParamDTO.class);
    }

    //根据spu中第三级商品分类查询出规格组集合及其下规格参数集合
    /*public List<SpecGroupDTO> findSpecGroupWithParamsByCid(Long id) {
        //根据分类id查询规格组集合
        List<SpecGroupDTO> specGroupDTOS = findSpecGroupByCid(id);
        //根据分类id查询规格参数集合
        List<SpecParamDTO> specParamDTOS = findSpecParam(null, id, null);
        //将规格参数集合使用流分组
        Map<Long, List<SpecParamDTO>> specParamMap = specParamDTOS.stream().collect(Collectors.groupingBy(SpecParamDTO::getGroupId));

        //遍历规格组参数
        specGroupDTOS.forEach(specGroupDTO -> {specGroupDTO.setParams(specParamMap.get(specGroupDTO.getId()));});
        return specGroupDTOS;


    }*/


    //根据spu中第三级商品分类查询出规格组集合及其下规格参数集合
    public List<SpecGroupDTO> findSpecGroupWithParamsByCid(Long id) {
        //根据分类id查询规格组集合
        List<SpecGroupDTO> specGroupDTOS = findSpecGroupByCid(id);

        //根据分类id查询规格参数集合
        List<SpecParamDTO> specParamDTOS = findSpecParam(id, null, null);

        //将规格参数集合使用流分组
        Map<Long, List<SpecParamDTO>> specParamMap = specParamDTOS.stream().collect(Collectors.groupingBy(SpecParamDTO::getGroupId));

        //遍历规格组集合
        specGroupDTOS.forEach(specGroupDTO -> {
            specGroupDTO.setParams(specParamMap.get(specGroupDTO.getId()));
        });

        return specGroupDTOS;

      }
}
